package com.xxja.order.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.http.ResponseEntity;

import java.util.List;

public class ResponseBuilder {

    private ResponseBuilder() {
    }

    /**
     * 相应任意 状态码
     *
     * @param status
     * @return
     */
    public static ResponseEntity allStatus(Integer status) {
        Integer handleStatus = defaultStatus(status);
        return ResponseEntity.status(handleStatus).build();
    }

    /**
     * 响应分页数据  任意状态码
     *
     * @param iPage
     * @param status
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity responseIpage(IPage<T> iPage, Integer status) {
        Integer handleStatus = defaultStatus(status);
        return ResponseEntity.status(handleStatus).header("total", new String[]{String.valueOf(iPage.getTotal())})
                .body(iPage.getRecords());
    }

    /**
     * 相应单个实体  任意状态码
     *
     * @param entity
     * @param status
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity responseEntity(T entity, Integer status) {
        Integer handleStatus = defaultStatus(status);
        return ResponseEntity.status(handleStatus).body(entity);
    }

    /**
     * 响应集合  任意状态码
     *
     * @param list
     * @param status
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity responseList(List<T> list, Integer status) {
        Integer handleStatus = defaultStatus(status);
        return ResponseEntity.status(handleStatus).body(list);
    }

    /**
     * 200  ok 不带任何body
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity ok() {
        return ResponseEntity.ok().build();
    }

    /**
     * 200 ok  带body
     *
     * @param body
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity okEntity(T body) {
        return ResponseEntity.ok(body);
    }


    /**
     * 设置 默认status 值为   200
     *
     * @param status
     * @return
     */
    private static Integer defaultStatus(Integer status) {
        if (status == null) {
            return 200;
        } else {
            return status;
        }
    }

}
